package com.boot.security.server.controller.system;

import java.util.List;

import com.boot.security.server.model.mffl.Vo.MfflConsultationVo;
import com.boot.security.server.service.impl.mffl.MfflConsultation.MfflConsultationService;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.mffl.MfflConsultationDao;
import com.boot.security.server.model.mffl.MfflConsultation;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@RestController
@RequestMapping("/mfflConsultations")
public class MfflConsultationController {

    @Autowired
    private MfflConsultationDao mfflConsultationDao;

    @Autowired
    private MfflConsultationService mfflConsultationService;

    @PostMapping
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAnyAuthority('sys:consultation:add')")
    public MfflConsultation save(@RequestBody MfflConsultation mfflConsultation) {
        mfflConsultationDao.save(mfflConsultation);

        return mfflConsultation;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    @PreAuthorize("hasAnyAuthority('sys:consultation:query')")
    public MfflConsultation get(@PathVariable Long id) {
        return mfflConsultationDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAnyAuthority('sys:consultation:add')")
    public MfflConsultation update(@RequestBody MfflConsultation mfflConsultation) {
        mfflConsultationDao.update(mfflConsultation);

        return mfflConsultation;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @PreAuthorize("hasAnyAuthority('sys:consultation:query')")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflConsultationDao.freeConsultationsCount(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflConsultationVo> list(PageTableRequest request) {
                return mfflConsultationDao.freeConsultationsList(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize("hasAnyAuthority('sys:consultation:del')")
    public void delete(@PathVariable Long id) {
        mfflConsultationService.delConsultationAndOrder(id);
    }
}
